const express = require('express');
const operation = require('../controller/operation')
const router = express.Router();
// 获取所有订单信息
router.get('/getInfo',(req,res)=>{
    let sql="select * from orderview_ad";

    operation.execute(sql,null).then(data=>{
        res.json({
            status:200,
            data,
            msg:'获取订单信息成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'获取单信息失败'
        })
    })
})
// 获取订单信息（分页）
router.get('/pagination',(req,res)=>{
    let params=req.query;
    let currentPage = params.currentPage||1;
    let pageSize = params.pageSize||6;
    let offset = (currentPage-1)*pageSize;
    let sql;

    let p1 = operation.getTotal('orderview_ad');
    sql =`select * from orderview_ad limit ${pageSize} offset ${offset}`;
    console.log(params);
    if(params.uid){
     sql =`select * from orderview_ad where uid=${params.uid} limit ${pageSize} offset ${offset} `;
     p1 = operation.getTotal('orderview_ad',{key:'uid',value:params.uid});
    }
    Promise.all([p1,operation.execute(sql,null)]).then(values=>{
    
        res.json({
            status:200,
            data:{ pagination:{currentPage,pageSize,total:values[0][0].total},data:values[1]},
            msg:'获取订单信息成功'  
        })
    }).catch(err=>{
        res.json({
            status:400,
            msg:'获取订单信息失败'  
        })
    })
})
// 订单模糊查询
router.get('/search',(req,res)=>{
    let pload = req.query;
    let query = pload.query;
    let currentPage = pload.currentPage||1;
    let pageSize = pload.pageSize||6;
    let offset = (currentPage-1)*pageSize;
    let sql =`SELECT * FROM orderview_ad WHERE uid LIKE '%${query}%' 
    or cid LIKE '%${query}%'
    or oid LIKE '%${query}%'
    or cname LIKE '%${query}%'
    or sname LIKE '%${query}%'
    or price LIKE '%${query}%'
    or username LIKE '%${query}%'`;
    let pageString=`limit ${pageSize} offset ${offset}`;
    Promise.all([operation.execute(sql,null),operation.execute(sql+pageString,null)]).then(values=>{
        res.json({
            status:200,
            data:{ pagination:{currentPage,pageSize,total:values[0].length},data:values[1]},
            msg:'查询相关到订单信息'  
        })
    }).catch(err=>{
        res.json({
            status:400,
            msg:'没有找到相关订单！'  
        })
    })

})
// 添加订单
router.post('/add',(req,res)=>{
    let data={uid,cid,sid,startTime,endTime,orderDate} = req.body;
    operation.addOrderList(data).then(ok=>{
        res.json({
            status:201,
            msg:ok
        })
    },err=>{
        res.json({
            status:400,
            msg:'err'
        })
    })
})
// 删除订单
router.delete('/remove',(req,res)=>{
    let oid = req.query.oid;
    operation.remove(oid,'oid','order_list').then(ok=>{
        res.json({
            status:200,
            msg:'删除成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'删除失败'
        })
    })
})
// 更改订单状态
router.put('/state',(req,res)=>{
    let data={oid,state} = req.body;
    let arr=[data.state,data.oid];
    let sql = `update order_list set state=? where oid=?`
    operation.execute(sql,arr).then(ok=>{
        res.json({
            status:200,
            msg:'修改成功'
        })
    }).catch(arr=>{
        res.json({
            status:400,
            msg:'更新失败'
        })
    })
})
module.exports = router;